package com.zmn.plat.business.impl.product.tariff;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.business.interfaces.product.tariff.ProductTariffBService;
import com.zmn.plat.model.entity.product.tariff.ProductTariff;
import com.zmn.plat.model.entity.product.tariff.ProductTariffQuery;
import com.zmn.plat.model.entity.product.tariff.group.ProductTariffGroup;
import com.zmn.plat.model.entity.product.tariff.group.ProductTariffGroupQuery;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;
import com.zmn.plat.model.vo.product.tariff.ProductTariffVO;
import com.zmn.plat.model.vo.product.tariff.group.ProductTariffGroupVO;
import com.zmn.plat.services.interfaces.product.tariff.ProductTariffService;
import com.zmn.plat.services.interfaces.product.tariff.group.ProductTariffGroupService;
import com.zmn.plat.services.interfaces.product.tariff.item.ProductTariffGroupItemService;
import com.zmn.plat.services.interfaces.product.tariff.item.city.price.ProductTariffItemCityPriceService;
import com.zmn.plat.services.interfaces.service.tariff.ServiceTariffService;

/**
 * 描述:新增和修改保存产品价格表和故障组组合业务接口
 *
 * @author linfeng
 * @since 16:58 2019/5/24
 **/
@Service
public class ProductTariffBServiceImpl implements ProductTariffBService {

    private Logger logger = LoggerFactory.getLogger(ProductTariffBServiceImpl.class);
    @Resource
    private ProductTariffService productFaultTariffService;
    @Resource
    private ProductTariffGroupService productFaultGroupService;
    @Resource
    private ProductTariffGroupItemService productFaultItemService;
    @Resource
    private ProductTariffItemCityPriceService productFaultItemCityPriceService;
    @Resource
    private ServiceTariffService faultTariffService;

    /**
     * 描述: 添加故障组组合
     *
     * @param productFaultTariffVO
     *            服务组组合数据
     * @param staff
     *            系统用户
     * @author linfeng
     * @since 16:57 2019/11/24
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProductFaultTariff(ProductTariffVO productFaultTariffVO, LoginStaff staff) {

        try {
            ProductTariff productFaultTariff = new ProductTariff();
            productFaultTariff.setCreater(staff.getRealName());
            productFaultTariff.setCreateTime(DateUtil.getNow());
            productFaultTariff.setUpdateTime(DateUtil.getNow());
            productFaultTariff.setUpdater(staff.getRealName());
            // productFaultTariff.setKeepType(productFaultTariffVO.getKeepType());
            productFaultTariff.setProductId(productFaultTariffVO.getProductId());
            productFaultTariff.setStatus(Objects.isNull(productFaultTariffVO.getStatus()) ? GlobalConsts.YES : productFaultTariffVO.getStatus());
            productFaultTariff.setDesc(productFaultTariffVO.getDesc());
            productFaultTariff.setGeneral(productFaultTariffVO.getGeneral());
            productFaultTariff.setSpecialDesc(productFaultTariffVO.getSpecialDesc());
            productFaultTariff.setTariffId(productFaultTariffVO.getTariffId());
            ServiceTariff faultTariff = faultTariffService.findByKey(productFaultTariffVO.getTariffId());
            productFaultTariff.setTariffName(faultTariff == null ? "" : faultTariff.getTariffName());
            productFaultTariffService.insert(productFaultTariff);

            productFaultTariffVO.getGroupIds().forEach(e -> {
                ProductTariffGroup faultGroup = new ProductTariffGroup();
                faultGroup.setGroupId(e);
                faultGroup.setProductId(productFaultTariffVO.getProductId());
                // faultGroup.setKeepType(productFaultTariffVO.getKeepType());
                faultGroup.setTariffId(productFaultTariffVO.getTariffId());
                faultGroup.setCreater(staff.getRealName());
                faultGroup.setCreateTime(DateUtil.getNow());
                faultGroup.setUpdateTime(DateUtil.getNow());
                faultGroup.setUpdater(staff.getRealName());
                productFaultGroupService.insert(faultGroup);
            });

        } catch (Exception e) {
            logger.error("添加故障组组合异常，数据回滚 Exception{}，msg{}", e, e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 描述: 修改故障组组合
     *
     * @param productFaultTariffVO
     *            故障组组合数据
     * @param staff
     *            系统用户
     * @author linfeng
     * @since 16:57 2019/11/24
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyProductFaultTariff(ProductTariffVO productFaultTariffVO, LoginStaff staff) {

        try {
            // 修改服务组组合数据
            ProductTariffQuery productFaultTariffQuery = new ProductTariffQuery();
            productFaultTariffQuery.setProductId(productFaultTariffVO.getProductId());
            productFaultTariffQuery.setTariffId(productFaultTariffVO.getTariffId());
            // productFaultTariffQuery.setBizType(productFaultTariffVO.getKeepType());
            List<ProductTariffVO> productFaultTariffs = productFaultTariffService.listByQuery(productFaultTariffQuery);
            ProductTariff productFaultTariff = new ProductTariff();
            if (productFaultTariffs != null && productFaultTariffs.size() > 0) {
                BeanUtils.copyProperties(productFaultTariffs.get(0), productFaultTariff);
            }
            productFaultTariff.setSpecialDesc(productFaultTariffVO.getSpecialDesc());
            productFaultTariff.setDesc(productFaultTariffVO.getDesc());
            productFaultTariff.setStatus(productFaultTariffVO.getStatus());
            productFaultTariff.setGeneral(productFaultTariffVO.getGeneral());
            productFaultTariff.setUpdateTime(new Date());
            productFaultTariff.setUpdater(staff.getRealName());
            productFaultTariffService.updateByKey(productFaultTariff);

            ProductTariffGroupQuery productFaultGroupQuery = new ProductTariffGroupQuery();
            productFaultGroupQuery.setProductId(productFaultTariffVO.getProductId());
            productFaultGroupQuery.setTariffId(productFaultTariffVO.getTariffId());
            List<ProductTariffGroupVO> productFaultGroupVOS = productFaultGroupService.listByQuery(productFaultGroupQuery);
            // 删除去掉关联关系的产品故障价格数据
            List<Integer> deletePriceGroupIds = new ArrayList<>();
            for (ProductTariffGroupVO productFaultGroupVO : productFaultGroupVOS) {
                Integer result = productFaultGroupVO.getGroupId();
                if (!productFaultTariffVO.getGroupIds().contains(result)) {
                    deletePriceGroupIds.add(result);
                }
            }

            // 找出重新勾选的项目组和之前的故障项目组差异 然后删除对应项目组的产品价格表数据 城市价格数据
            if (deletePriceGroupIds.size() > 0) {
                productFaultItemService.deleteByProductIdAndTariffIdAndGroupIds(productFaultTariffVO.getProductId(), productFaultTariffVO.getTariffId(), deletePriceGroupIds);
                productFaultItemCityPriceService.deleteByProductIdAndTarifIdAndGroupIds(productFaultTariffVO.getProductId(), productFaultTariffVO.getTariffId(), deletePriceGroupIds);
            }

            // 删除关系数据
            productFaultGroupService.deleteByProductIdAndTariffId(productFaultTariffVO.getProductId(), productFaultTariffVO.getTariffId());
            productFaultTariffVO.getGroupIds().forEach(e -> {
                ProductTariffGroup faultGroup = new ProductTariffGroup();
                faultGroup.setGroupId(e);
                faultGroup.setProductId(productFaultTariffVO.getProductId());
                // faultGroup.setKeepType(productFaultTariffVO.getKeepType());
                faultGroup.setTariffId(productFaultTariffVO.getTariffId());
                faultGroup.setCreater(staff.getRealName());
                faultGroup.setCreateTime(DateUtil.getNow());
                faultGroup.setUpdateTime(DateUtil.getNow());
                faultGroup.setUpdater(staff.getRealName());
                productFaultGroupService.insert(faultGroup);
            });

        } catch (Exception e) {
            logger.error("修改故障组组合异常，数据回滚 Exception{}，msg{}", e, e.getMessage());
            throw new RuntimeException(e);
        }
    }
}